/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.service;

import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.manage.core.manage.convertor.PositionManageConvertor;
import info.peigen.hotpot.manage.core.manage.form.PositionCreateForm;
import info.peigen.hotpot.manage.core.manage.form.PositionEditForm;
import info.peigen.hotpot.manage.core.manage.form.PositionSearchForm;
import info.peigen.hotpot.manage.core.manage.repository.service.PositionManageRepository;
import info.peigen.hotpot.manage.core.repository.entity.PositionEntity;
import info.peigen.hotpot.manage.core.service.PositionService;
import info.peigen.hotpot.manage.core.service.order.PositionCreateOrder;
import info.peigen.hotpot.manage.core.service.order.PositionEditOrder;
import info.peigen.hotpot.manage.core.service.result.PositionCreateResult;
import info.peigen.hotpot.manage.core.service.result.PositionEditResult;
import info.peigen.hotpot.manage.core.service.result.info.PositionInfo;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * <b>(PositionManageService)</b>
 * 管理后台 "职位" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-05
 */
@SuppressWarnings("unused")
@Component
public class PositionManageService {
    @Inject
    PositionManageRepository positionManageRepository;
    @Inject
    PositionService          positionService;

    public PageResult<PositionInfo> page(PositionSearchForm form) {
        LambdaQuery<PositionEntity> lambdaQuery = query(form);
        return positionManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, PositionManageConvertor.INSTANCE::convertToInfo);
    }

    public List<PositionInfo> list(PositionSearchForm form) {
        LambdaQuery<PositionEntity> lambdaQuery = query(form);
        List<PositionEntity>        entities    = lambdaQuery.select();
        return entities.stream().map(PositionManageConvertor.INSTANCE::convertToInfo).toList();
    }

    public PositionInfo create(PositionCreateForm form) {
        PositionCreateOrder  order  = PositionManageConvertor.INSTANCE.convertToCreateOrder(form);
        PositionCreateResult result = positionService.create(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public PositionInfo edit(PositionEditForm form) {
        PositionEditOrder  order  = PositionManageConvertor.INSTANCE.convertToEditOrder(form);
        PositionEditResult result = positionService.edit(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public void remove(Integer id) {
        positionManageRepository.delete(id);
    }

    public void removes(List<Integer> ids) {
        positionManageRepository.removeByIds(ids);
    }

    public PositionInfo detail(Integer id) {
        PositionEntity entity = positionManageRepository.getMapper().single(id);
        return PositionManageConvertor.INSTANCE.convertToInfo(entity);
    }

    public PositionInfo detail(String positionId) {
        PositionEntity entity = positionManageRepository.getMapper().findByPositionId(positionId);
        return PositionManageConvertor.INSTANCE.convertToInfo(entity);
    }

    private LambdaQuery<PositionEntity> query(PositionSearchForm form) {
        return positionManageRepository.getMapper().createLambdaQuery()
                .andLike(PositionEntity::getPositionId, Query.filterLikeEmpty(form.getPositionId(), true, true))
                .andLike(PositionEntity::getOrganizationId, Query.filterLikeEmpty(form.getOrganizationId(), true, true))
                .andLike(PositionEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), true, true))
                .desc(form.getOrderName())
                ;
    }

}